/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : booking_list.js                            
*  Description  : 마이홈 부킹
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-11  김효순    Created.  
*  2012-05-12  김효순    Complete.
*******************************************************************************/

jQuery(function($){
	// 조인룸
	function clsJoinroomList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsJoinroomList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home_right article.joinroom_list");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("p.total:first, dl:first").clone(true)
			}
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("p.total").each(function(n){
				var szText = "";
				$(this).contents().filter(function(){
					return (this.nodeType == 3);
				}).remove();
				$(this).find("span:first~em").remove();
				switch(thisClass.listData[n]["내상태"]){
					case "부킹장": 
						$(this).find("span:eq(0)").attr("class", "ft_b").html("회원님<em>(부킹장)</em>");
						if(thisClass.listData[n]["참여자"].length > 1){
							szText = "외 <em>" + thisClass.listData[n]["참여자"].length + "</em>명 <em>부킹참여 예정</em>";
						}else{
							szText = " <em>부킹참여 예정</em>";
						}
						break;
					case "참여완료":
						$(this).find("span:eq(0)").attr("class", "ft_r").html("[" + thisClass.listData[n]["내상태"] + "]");
						szText = " 회원님 외 <em>" + thisClass.listData[n]["참여자"].length + "</em>명 <em>부킹참여 예정</em>";
						$(this).find("img[alt='조인룸 취소']").parent("a").hide();
						break;
					case "신청중":
						$(this).find("span:eq(0)").attr("class", "ft_r").html("[" + thisClass.listData[n]["내상태"] + "]");
						szText = "";
						$(this).find("img[alt='조인룸 취소']").parent("a").hide();
						break;
				}
				$(this).find("span:first").after(szText);
			}).next("dl").each(function(n){
				$(this).find("img:first").attr("src", thisClass.listData[n]["부킹장"]["이미지"]);
				if(!thisClass.listData[n]["비밀방"]) $(this).find("img[alt='비밀글']").hide();
				$(this).find("p.time a").html(thisClass.listData[n]["날짜"] + "(" + thisClass.listData[n]["요일"] + ")" + thisClass.listData[n]["시간"]);
				$(this).find("p.id").html(thisClass.listData[n]["골프장"]);
				$(this).find("p.join_cor").html(thisClass.listData[n]["코스명"] + " " + thisClass.listData[n]["홀명"]);
				$(this).find("div.nickname a").html(thisClass.listData[n]["부킹장"]["닉네임"]);
				$(this).find("div.offer:eq(0) span").html(thisClass.listData[n]["참여자"].length).hover(
					function(){
						document.objGlobal.objJoinroomLayer.open($(this), thisClass.listData[n]["참여자"], "참여취소", -255, 10);
					}
					, function(){
						document.objGlobal.objJoinroomLayer.hide();
					}
				);
				$(this).find("div.offer:eq(1) span").html(thisClass.listData[n]["신청자"].length).hover(
					function(){
						document.objGlobal.objJoinroomLayer.open($(this), thisClass.listData[n]["신청자"], "참여승인", -255, 10);
					}
					, function(){
						document.objGlobal.objJoinroomLayer.hide();
					}
				);
				$(this).find("div.room_txt").html(thisClass.listData[n]["내용"]);
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p, dl").hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='position:relative; width:350px; height:100px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div#tmpSpin").hide().spin(false);
			$layer.find("p, dl").show();
		}
		// 조인룸 취소버튼
		, $getBtnCancel: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("img[alt='조인룸 취소']");
		}
		, getJoinroomKey: function(idx){
			return this.listData[idx]["조인룸키"];
		}
		, cancelSpin: function(n){
			var $layer = this._HTMLDom["$layer"];

			$layer.find("img[alt='조인룸 취소']").eq(n).hide().after("<div id='tmpBtnSpin' style='position:relative; width:80px; height:15px;' />");
			$layer.find("img[alt='조인룸 취소']").eq(n).next("div#tmpBtnSpin").spin({
				"length": 5
				, "width": 3
				, "radius": 4
			});
		}
	}
	
	// 예약현황
	function clsReserveList()
	{
		this._HTMLDom = null;
		this.listData = null;
		this.idx = null;
		
		this._init();
	}
	
	clsReserveList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home_left div.reserve_list02:eq(0) tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element1": $layer.find("tr:eq(0)").clone(true)
				, "$element2": $layer.find("tr:eq(1)").hide().clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element1 = this._HTMLDom["$element1"];
			var $element2 = this._HTMLDom["$element2"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element1.clone(true)).append($element2.clone(true));
			}
			$layer.find("tr:even").each(function(n){
				$(this).find("td:eq(0) a").html(thisClass.listData[n]["예약일"].substr(5) + "(" + thisClass.listData[n]["예약요일"] + ") " + thisClass.listData[n]["예약시간"]);
				$(this).find("td:eq(1) strong").html(thisClass.listData[n]["골프장"]);
				$(this).find("td:eq(2)").html(thisClass.listData[n]["코스명"]);
				$(this).find("td:eq(3)").html(thisClass.listData[n]["홀명"]);
				$(this).find("td:eq(4)").html(thisClass.listData[n]["부킹장"]);
				
				var a = thisClass.listData[n]["동반자알림"].split("/");
				$(this).find("td:eq(5)").html(a[0] + "<span>/" + a[1] + "건</span>");
			}).next("tr").each(function(n){
				$(this).find("div.left p:eq(0)").html(thisClass.listData[n]["골프장"] + " " + thisClass.listData[n]["코스명"] + " " + thisClass.listData[n]["홀명"]);
				$(this).find("div.left p:eq(1)").contents().eq(0).before(thisClass.listData[n]["예약일"] + " " + thisClass.listData[n]["예약요일"] + "요일 ").remove();
				$(this).find("div.left p:eq(1) em").html(thisClass.listData[n]["예약시간"]);
				$(this).find("div.left p:eq(2) em").html(thisClass.listData[n]["원가"]);
				$(this).find("div.left p:eq(3) em").html(thisClass.listData[n]["가격"]);
				
				$(this).find("div.right ul.group").find("li").each(function(i){
					if(thisClass.listData[n]["이벤트"].indexOf($(this).text())== -1) $(this).html("");
				});
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent("table").next("div#tmpSpin").length == 0){
				$layer.parent("table").after("<div id='tmpSpin' style='position:relative; width:600px; height:70px;' />");
			}
			$layer.parent("table").next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent("table").next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
		, setIdx: function(idx){
			if(this.idx == idx) return;
			
			this.idx = idx;
			this.show();
		}
		, show: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("tr:odd").hide()
			$layer.find("tr:odd").eq(this.idx).show("slow");
		}
		, $getBtnCancel: function(){
			return this._HTMLDom["$layer"].find("img[alt='취소하기']");
		}
		, $getBtnSms: function(){
			return this._HTMLDom["$layer"].find("img[alt='SMS 알림']");
		}
		, $getBtnEmail: function(){
			return this._HTMLDom["$layer"].find("img[alt='EMAIL 알림']");
		}
		, cancelSpin: function(n){
			this._HTMLDom["$layer"].find("img[alt='취소하기']").eq(n).hide().parents("td:first").spin({
				"length": 5
				, "width": 3
				, "radius": 4
			});
		}
		, getReserveKey: function(){
			var idx = null;
			
			if(arguments[0]){
				idx = arguments[0];
			}else{
				idx = this.idx;
			}
			
			if(idx){
				return this.listData[idx]["예약키"];
			}else{
				return null;
			}
		}
		, $getBtnShows: function(){
			return this._HTMLDom["$layer"].find("tr:even td:first-child");
		}
	}
	
	// 서버통신
	function clsComm()
	{
		// 예약현황
		this.reserveList = null;
		// 조인룸
		this.joinroomList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.reserveList = [
				{
					"예약키": null
					, "예약일": null
					, "예약요일": null
					, "예약시간": null
					, "골프장": null
					, "코스명": null
					, "홀명": null
					, "부킹장": null
					, "동반자알림": null
					, "이벤트": null
					, "원가": null
					, "가격": null
				}
			];
			
			this.joinroomList = [
				{
					"조인룸키": null
					, "부킹장": {
						"이미지": null
						, "아이디": null
						, "닉네임": null
					}
					, "비밀방": null
					, "날짜": null
					, "요일": null
					, "시간": null
					, "골프장": null
					, "코스명": null
					, "홀명": null
					, "내용": null
					, "참여자": [
						{
							"아이디": null
							, "이미지": null
							, "닉네임": null
						}
					]
					, "신청자": [
						{
							"아이디": null
							, "이미지": null
							, "닉네임": null
						}
					]
					, "내상태": null
				}
			];
		}
		, getReserveList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.reserveList = [
					{
						"예약키": "001"
						, "예약일": "2012.04.21"
						, "예약요일": "수"
						, "예약시간": "06:31"
						, "골프장": "SKY71"
						, "코스명": "하늘코스1"
						, "홀명": "11H"
						, "부킹장": "타이거우즈1"
						, "동반자알림": "1/3"
						, "이벤트": "그린피, 카트피, 식사, 선물, 기타"
						, "원가": "123,001"
						, "가격": "1,090,001"
					}
					, {
						"예약키": "002"
						, "예약일": "2012.04.22"
						, "예약요일": "월"
						, "예약시간": "06:32"
						, "골프장": "SKY72"
						, "코스명": "하늘코스2"
						, "홀명": "12H"
						, "부킹장": "타이거우즈2"
						, "동반자알림": "2/3"
						, "이벤트": "그린피, 선물, 기타"
						, "원가": "123,002"
						, "가격": "1,090,002"
					}
					, {
						"예약키": "003"
						, "예약일": "2012.04.23"
						, "예약요일": "화"
						, "예약시간": "06:33"
						, "골프장": "SKY73"
						, "코스명": "하늘코스3"
						, "홀명": "13H"
						, "부킹장": "타이거우즈3"
						, "동반자알림": "3/3"
						, "이벤트": "그린피, 카트피, 식사, 기타"
						, "원가": "123,003"
						, "가격": "1,090,003"
					}
				];
				
				document.objControl.finishReserveList();
			}, 1000);
		}
		, getReserveData: function(){
			return this.reserveList;
		}
		, reserveCancel: function(reserveKey){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				document.objControl.finishReserveCancel();
			}, 1000);
		}
		, getJoinroomList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.joinroomList = [
					{
						"조인룸키": "001"
						, "부킹장": {
							"이미지": "../images/board/default_50_50.gif"
							, "아이디": "sky71"
							, "닉네임": "김밥아저씨001"
						}
						, "비밀방": true
						, "날짜": "03.03"
						, "요일": "월"
						, "시간": "09:31"
						, "골프장": "Sky71"
						, "코스명": "오션코스1"
						, "홀명": "11H"
						, "내용": "여자들만 칩니다. 강미모인들은 모여주세요~<br/>비밀번호는 동호회에~1"
						, "참여자": [
							{
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "필드광1"
							}
						]
						, "신청자": [
							{
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "필드광4"
							}
							, {
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "오스케5"
							}
							, {
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "강피엠골프짱6"
							}
						]
						, "내상태": "부킹장"
					}
					, {
						"조인룸키": "002"
						, "부킹장": {
							"이미지": "../images/board/default_50_50.gif"
							, "아이디": "sky72"
							, "닉네임": "김밥아저씨002"
						}
						, "비밀방": false
						, "날짜": "03.04"
						, "요일": "화"
						, "시간": "09:32"
						, "골프장": "Sky72"
						, "코스명": "오션코스2"
						, "홀명": "12H"
						, "내용": "여자들만 칩니다. 강미모인들은 모여주세요~<br/>비밀번호는 동호회에~2"
						, "참여자": [
							{
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "필드광7"
							}
							, {
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "오스케8"
							}
						]
						, "신청자": [
							{
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "필드광1"
							}
							, {
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "오스케2"
							}
						]
						, "내상태": "참여완료"
					}
					, {
						"조인룸키": "003"
						, "부킹장": {
							"이미지": "../images/board/default_50_50.gif"
							, "아이디": "sky73"
							, "닉네임": "김밥아저씨003"
						}
						, "비밀방": true
						, "날짜": "03.05"
						, "요일": "수"
						, "시간": "09:33"
						, "골프장": "Sky73"
						, "코스명": "오션코스3"
						, "홀명": "13H"
						, "내용": "여자들만 칩니다. 강미모인들은 모여주세요~<br/>비밀번호는 동호회에~3"
						, "참여자": [
							{
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "필드광4"
							}
							, {
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "오스케5"
							}
							, {
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "강피엠골프짱6"
							}
						]
						, "신청자": [
							{
								"아이디": "000"
								, "이미지": "../images/board/default_35_35.gif"
								, "닉네임": "필드광7"
							}
						]
						, "내상태": "신청중"
					}
				];
				
				document.objControl.finishJoinroomList();
			}, 1500);
		}
		, getJoinroomData: function(){
			return this.joinroomList;
		}
		, joinroomCancel: function(joinroomKey){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				var ret = {
					"결과코드": "001"
					, "메시지": null
				}
				
				document.objControl.finishJoinroomCancel(ret);
			}, 1000);
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objReserveList = null;
		this.objJoinroomList = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objReserveList = new clsReserveList();
			this.objJoinroomList = new clsJoinroomList();
			
			this._bind();
			
			// 예약현황
			this.objReserveList.spinStart();
			this.objComm.getReserveList();
			
			// 조인룸
			this.objJoinroomList.spinStart();
			this.objComm.getJoinroomList();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 취소하기
			this.objReserveList.$getBtnCancel().live("click", function(){
				var idx = thisClass.objReserveList.$getBtnCancel().index($(this));
				
				thisClass.objReserveList.cancelSpin(idx);
				thisClass.objComm.reserveCancel(thisClass.objReserveList.getReserveKey(idx));
			});
			
			// SMS 발송 클릭
			this.objReserveList.$getBtnSms().live("click", function(){
				var winSms = window.open("../popup/pop_sms.html", "winSms", "width=400, height=300, scrollbars=0");
				if(winSms) winSms.focus();
			});
			
			// Email 발송 클릭
			this.objReserveList.$getBtnEmail().live("click", function(){
				var winEmail = window.open("../popup/pop_email.html", "winEmail", "width=700, height=300, scrollbars=0");
				if(winEmail) winEmail.focus();
			});
			
			// 예약현황 클릭
			this.objReserveList.$getBtnShows().live("click", function(){
				thisClass.objReserveList.setIdx(thisClass.objReserveList.$getBtnShows().index($(this)));
			});
			
			// 조인룸취소
			this.objJoinroomList.$getBtnCancel().live("click", function(){
				var idx = thisClass.objJoinroomList.$getBtnCancel().index($(this));
				
				thisClass.objJoinroomList.cancelSpin(idx);
				thisClass.objComm.joinroomCancel(thisClass.objJoinroomList.getJoinroomKey(idx));
			});
		}
		// 예약현황 완료
		, finishReserveList: function(){
			var thisData = this.objComm.getReserveData();
			
			this.objReserveList.spinEnd();
			this.objReserveList.setList(thisData);
		}
		// 예약취소 완료
		, finishReserveCancel: function(){
			this.objReserveList.spinStart();
			this.objComm.getReserveList();
		}
		// 조인룸 완료
		, finishJoinroomList: function(){
			var thisData = this.objComm.getJoinroomData();
				
			this.objJoinroomList.spinEnd();
			this.objJoinroomList.setList(thisData);
		}
		// 조인룸 취소 완료
		, finishJoinroomCancel: function(ret){
			this.objJoinroomList.spinStart();
			this.objComm.getJoinroomList();
		}
	}
	
	document.objControl = new clsControl();
});
